#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>

// spfa
using namespace std;

const int N = 410;

int n, m;
int h[N], e[N], ne[N], w[N], idx;
int dist[N];
int state[N];

void add(int a, int b, int c){
    e[idx] = b;
    w[idx] = c;
    ne[idx] = h[a];
    h[a] = idx++;
}

int spfa(){
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;
    queue<int> que;
    que.push(1);
    state[1] = true;

    while(que.size()){
        int k = que.front();
        que.pop();
        state[k] = false;

        for (int u = h[k]; u != -1; u = ne[u]){
            int j = e[u];

            if(dist[j] > dist[k] + w[u]){
                dist[j] = dist[k] + w[u];
                if(!state[j]){
                    que.push(j);
                    state[j] = true;
                }
            }
        }

    }

    int minv = 0;
    for (int i = 1; i <= n; i ++){
        minv = max(minv, dist[i]);
    }

    if(minv == 0x3f3f3f3f){
        return -1;
    }else{
        return minv;
    }
}



int main()
{    
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);

    cin >> n >> m;
    memset(h, -1, sizeof h);
    for (int i = 0; i < m; i++){
        int a, b, c;
        cin >> a >> b >> c;
        add(a, b, c);
        add(b, a, c);
    }

    int k = spfa();
    cout << k << endl;
    return 0;
}